import random
from scrapy.exceptions import IgnoreRequest
from scrapy import log

class RandomUserAgent(object):

    def __init__(self, agents):
        self.agents = agents

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings.getlist('USER_AGENTS'))

    def process_request(self, request, spider):
        agent = random.choice(self.agents)
        #log.msg('User-Agent:%s'%agent)
        request.headers.setdefault('User-Agent', random.choice(self.agents))
